package com.tbit.uqbike.service.business.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tbit.common.entity.view.PageResult;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.DepositApply;
import com.tbit.uqbike.service.business.DepositApplyService;
import com.tbit.uqbike.service.business.ReturnDepositService;
import com.tbit.uqbike.webmanager.dao.core.DepositApplyDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @description:
 * @author: chen
 * @create: 2024-01-09 11:27
 **/
@Service
public class DepositApplyServiceImpl extends ServiceImpl<DepositApplyDao, DepositApply> implements DepositApplyService {

    @Autowired
    private DepositApplyDao depositApplyDao;
    @Autowired
    private ReturnDepositService returnDepositService;


    @Override
    public PageResult<DepositApply> queryPage(Integer accountId,String phone, Long startTime, Long endTime, String handelPhone, Integer state
            ,Integer page, Integer size) {
        QueryWrapper<DepositApply> queryWrapper = new QueryWrapper<DepositApply>();
        queryWrapper.eq("accountId",accountId);
        if(StringUtils.isNotBlank(phone)){
            queryWrapper.eq("phone",phone);
        }
        if(startTime!=null){
            queryWrapper.ge("addTime",new Date(startTime));
        }
        if(endTime != null){
            queryWrapper.le("addTime",new Date(endTime));
        }
        if(StringUtils.isNotBlank(handelPhone)){
            queryWrapper.eq("handelPhone",handelPhone);
        }
        if(state!=null){
            queryWrapper.eq("state",state);
        }
        PageInfo<DepositApply> pageInfo= PageHelper.startPage(page,size).doSelectPageInfo(()->
                list(queryWrapper)
        );
        return new PageResult<>(page, size, pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getList());
    }

    @Override
    //@Transactional
    public void editState(Integer applyId,Integer state, String refundNO, Integer handleUserId, String handelPhone,String handelName) {

        DepositApply depositApplyDB= Optional.ofNullable(getById(applyId)).orElseThrow(() ->new BaseException("apply_not_exist")) ;
        if(depositApplyDB.getState().equals(1)){
            throw new BaseException("apply_is_exist");
        }
        Date date = new Date();
        returnDepositService.manualDepositRefund(depositApplyDB.getUserId(), "押金申请审核通过退除押金", date,handleUserId,handelPhone,handelName);
        DepositApply depositApply = new DepositApply();
        depositApply.setApplyId(applyId);
        depositApply.setState(state);
        depositApply.setRefundNO(refundNO);
        depositApply.setHandleUserId(handleUserId);
        depositApply.setHandleTime(date);
        depositApply.setHandelPhone(handelPhone);
        depositApplyDao.editState(depositApply);
    }

    @Override
    public List<DepositApply> selectList(Integer userId, Integer brandId, Date startTime, Date endTime) {
        QueryWrapper<DepositApply> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(DepositApply::getUserId, userId)
                .eq(DepositApply::getAccountId, brandId)
                .ge(Objects.nonNull(startTime), DepositApply::getAddTime, startTime)
                .le(Objects.nonNull(endTime), DepositApply::getAddTime, endTime);
        return depositApplyDao.selectList(wrapper);
    }
}